package com.example.webchar.webchar.controller;

import com.example.webchar.webchar.pojo.Users;
import com.example.webchar.webchar.service.UserService;
import com.example.webchar.webchar.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: webchars
 * @description
 * @author: 毛叶贵子
 * @create: 2021-08-19 22:52
 **/
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据user信息进行登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Users user) {
        if (user.getName() == null || user.getPassword() == null) {
            return Result.fail().add("err", "账号或者密码为空");
        } else {
            String token = userService.toLogin(user);
            if (token != null) {
                return Result.success().add("token", token);
                //将token信息返回

            } else {
                return Result.fail().add("err", "账号密码有误");
            }

        }
    }

    /**
     * token信息错误界面
     *
     * @return
     */
    @GetMapping("/401")
    public Result err() {
        return Result.fail().add("err", "缺失token信息");
    }

    /**
     * 权限不合适界面
     *
     * @return
     */

    @GetMapping("/403")
    public Result errUnauthorized() {
        return Result.fail().add("err", "权限不合适");
    }


}
